type Handler = (data?: any) => void

const _getSystemInfo = (uni: any, handler: Handler) => {
	uni.getSystemInfo({
		success: (res: any) => {
		  const statusBarHeight = res.statusBarHeight || 0
		  const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
		  const navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2
		  const topIconDistance = statusBarHeight + navBarHeight
		  const h = res.screenHeight
		  handler({ safeTop: topIconDistance, height: h })
		}
	})
}

const _setStorage = (uni: any, key: string, value: string, handler?: Handler) => {
	uni.setStorage({
		key,
		data: value,
		success: handler
	})
}

const _getStorage = (uni: any, key: string, handler?: Handler) => {
	uni.getStorage({
		key,
		success: (res: any) => {
			handler && handler(res.data)	
		}
	})
}




export default class UniApp {
	private ctx: any
	static url = '127.0.0.1:8080'
	constructor(ctx: any) {
		this.ctx = ctx
	}
	system(handler: Handler) {
		_getSystemInfo(this.ctx, handler)
	}
	set(key: string, value: any, handler: Handler) {
		_setStorage(this.ctx, key, value, handler);
	}
	get(key: string, handler: Handler) {
		_getStorage(this.ctx, key, handler);
	}
}
